package com.sch.hsy.web.controller.system;

import com.sch.hsy.common.core.controller.BaseController;
import com.sch.hsy.common.core.domain.AjaxResult;
import com.sch.hsy.common.core.domain.Ztree;
import com.sch.hsy.common.core.domain.entity.SysBook;
import com.sch.hsy.common.core.domain.entity.SysChapter;
import com.sch.hsy.common.core.page.TableDataInfo;
import com.sch.hsy.common.utils.file.FileUploadUtils;
import com.sch.hsy.system.domain.SysQues;
import com.sch.hsy.system.service.ISysBookService;
import com.sch.hsy.system.service.ISysChapterService;
import com.sch.hsy.system.service.ISysDeptService;
import com.sch.hsy.system.service.ISysQuesService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 书籍管理
 *
 */
@Controller
@RequestMapping("/chapter")
public class SysChapterController extends BaseController {

    private String prefix = "book";

    @Autowired
    private ISysChapterService chapterService;

    @Autowired
    private ISysQuesService quesService;


    @Value("${file.upload.path}")
    private String filePath;



    /**
     * 根据书籍查看章节列表
     *
     */
    @GetMapping(value = { "/list/{bookId}"})
    public String chapterList(@PathVariable("bookId") Long bookId,
                                 ModelMap mmap)
    {
        mmap.put("bookId", bookId);
        mmap.put("list", chapterService.selectChapterList(bookId));
        return prefix + "/chapters";
    }

    /**
     * 查询章节列表
     *
     */
    @PostMapping(value = { "/refreshlist"})
    @ResponseBody
    public TableDataInfo list(Long bookId)
    {
        startPage();
        List<SysChapter> list = chapterService.selectChapterList(bookId);
        return getDataTable(list);
    }

    /**
     * 新增章节页面
     */
    @GetMapping("/addChapter/{bookId}")
    public String addBook(@PathVariable("bookId") Long bookId,ModelMap mmap)
    {
        mmap.put("bookId", bookId);
        return prefix + "/addChapter";
    }

    /**
     * 新增信息
     */
    @PostMapping("/addCha")
    @ResponseBody
    public AjaxResult addCha(MultipartFile file, SysChapter chapter) throws IOException {
        if (file != null) {
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            chapter.setFileName(fileName);
        }
        return toAjax(chapterService.insertChapter( chapter));
    }

    @PostMapping("/removeChapter")
    @ResponseBody
    public AjaxResult removeChapter(Long ids)
    {

        return toAjax(chapterService.deleteChapterById(ids));
    }

    /**
     * 章节问题设置
     */
    @GetMapping("/addQues/{id}")
    public String addQues(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysQues sysQues = new SysQues();
        sysQues.setChapterId(id);
        List<SysQues> list = quesService.selectQuesList(sysQues);
        mmap.put("ques", list);
        mmap.put("chapterId", id);
        return prefix + "/addQ";
    }

    /**
     * 问题清单
     */
    @GetMapping("/ques/list/{id}")
    @ResponseBody
    public List<SysQues> quesList(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysQues sysQues = new SysQues();
        sysQues.setChapterId(id);
        List<SysQues> list = quesService.selectQuesList(sysQues);
        return list;
    }

    @PostMapping("/ques/add")
    @ResponseBody
    public AjaxResult addQues(SysQues ques)
    {
        return toAjax(quesService.insertQues(ques));
    }

    @PostMapping("/removeQues")
    @ResponseBody
    public AjaxResult removeQues(Long id)
    {

        return toAjax(quesService.deleteQuesById(id));
    }



}
